/**
 *
 * Licensed Materials - Property of IBM 
 * 
 * OCO Source Materials 
 * 
 * (C) Copyright IBM Corp. 2011, 2012 All Rights Reserved 
 * 
 * The source code for this program is not published or other- 
 * wise divested of its trade secrets, irrespective of what has 
 * been deposited with the U.S. Copyright Office. 
 *
 */

/**
 * CHANGE HiSTORY:
 * Author             Time                 Description
 *
 */
package com.ibm.ba.demo.service;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.ibm.ba.demo.dao.InDBScoreDao;
import com.ibm.ba.demo.dao.ProductDao;
import com.ibm.ba.demo.entity.BaDemoScoreResult;
import com.ibm.ba.demo.entity.SPSSModelConfiguration;
import com.ibm.ba.demo.exception.GeneralException;
import com.ibm.ba.demo.helper.BeanHelper;

@Service
public class InDBScoreServiceImpl implements ScoreService {

	private static Logger logger = Logger
			.getLogger(InDBScoreServiceImpl.class);

	@Autowired
	private InDBScoreDao scoreDao;
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private SPSSModelConfiguration modelConfig;
	
	@Autowired
	private InDBScoreRequestServiceImpl scoreRequestService;

	public BaDemoScoreResult getScore(int customerId)
			throws GeneralException {
		BaDemoScoreResult scoreResult = null;
		try {
			Map<String, Object> resultMap = productDao
					.getProductIdsByCustomerId(customerId);
			scoreResult = scoreDao.getScore(scoreRequestService.convert(resultMap));
			scoreResult.setProducts(productDao.getProductNames(scoreResult.getProducts()));
			
		} catch (DataAccessException e) {
			logger.error("ba.error.00004 - UDF call: get score failed[scoreRequest:"
							+ BeanHelper.beanToJson(customerId) + "]", e);
			throw new GeneralException("ba.error.00004", e);
		} catch (Exception e) {
			logger.error("ba.error.00001 - UDF call: get score failed[scoreRequest:"
							+ BeanHelper.beanToJson(customerId) + "]", e);
			throw new GeneralException("ba.error.00001", e);
		}
		return scoreResult;
	}

}
